<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/blog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/blog/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.2/css/all.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"littlefxc.github.io","root":"/blog/","images":"/blog/images","scheme":"Mist","version":"8.2.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/blog/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}};
  </script>
<meta name="description" content="1 环境搭建 3台服务器  centos 7  采用 yum 方式，在其中两台安装 mysql  检查mysql 安装是否正确  下载 Mycat 软件包  在第3台机器上安装mycat，并修改配置文件   连接mycat，体验数据的增删改查   2 mysql 安装教程2.1 查询是否安装了mysql**  1rpm -qa|grep mysql">
<meta property="og:type" content="article">
<meta property="og:title" content="搭建Mycat">
<meta property="og:url" content="http://littlefxc.github.io/2021/06/09/%E6%90%AD%E5%BB%BAMycat/index.html">
<meta property="og:site_name" content="一年春又来">
<meta property="og:description" content="1 环境搭建 3台服务器  centos 7  采用 yum 方式，在其中两台安装 mysql  检查mysql 安装是否正确  下载 Mycat 软件包  在第3台机器上安装mycat，并修改配置文件   连接mycat，体验数据的增删改查   2 mysql 安装教程2.1 查询是否安装了mysql**  1rpm -qa|grep mysql">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/littlefxc/oss/raw/master/images/image-20210609210635587.png">
<meta property="og:image" content="https://gitee.com/littlefxc/oss/raw/master/images/5e81a3ba087c42ce05540189.jpg">
<meta property="og:image" content="https://gitee.com/littlefxc/oss/raw/master/images/image-20210610162658349.png">
<meta property="og:image" content="https://gitee.com/littlefxc/oss/raw/master/images/image-20210610163531656.png">
<meta property="og:image" content="https://gitee.com/littlefxc/oss/raw/master/images/image-20210611093234112.png">
<meta property="article:published_time" content="2021-06-09T12:51:05.000Z">
<meta property="article:modified_time" content="2021-07-08T07:54:25.351Z">
<meta property="article:author" content="一年春又来">
<meta property="article:tag" content="读写分离">
<meta property="article:tag" content="分库分表">
<meta property="article:tag" content="mycat">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/littlefxc/oss/raw/master/images/image-20210609210635587.png">


<link rel="canonical" href="http://littlefxc.github.io/2021/06/09/%E6%90%AD%E5%BB%BAMycat/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>
<title>搭建Mycat | 一年春又来</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

<link rel="alternate" href="/blog/atom.xml" title="一年春又来" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/blog/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">一年春又来</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/blog/" rel="section"><i class="home                          //首页 fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-archives"><a href="/blog/archives/" rel="section"><i class="archive          //归档 fa-fw"></i>归档</a></li>
        <li class="menu-item menu-item-categories"><a href="/blog/categories/" rel="section"><i class="th           //分类 fa-fw"></i>分类</a></li>
        <li class="menu-item menu-item-tags"><a href="/blog/tags/" rel="section"><i class="tags                     //标签 fa-fw"></i>标签</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#1-%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA"><span class="nav-text">1 环境搭建</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#2-mysql-%E5%AE%89%E8%A3%85%E6%95%99%E7%A8%8B"><span class="nav-text">2 mysql 安装教程</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#2-1-%E6%9F%A5%E8%AF%A2%E6%98%AF%E5%90%A6%E5%AE%89%E8%A3%85%E4%BA%86mysql"><span class="nav-text">2.1 查询是否安装了mysql**</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-2-%E5%8D%B8%E8%BD%BDmysql-%EF%BC%88%E4%B8%8B%E9%9D%A2%E6%98%AF%E5%8D%B8%E8%BD%BDmysql%E7%9A%84%E5%BA%93%EF%BC%8C%E9%98%B2%E6%AD%A2%E4%BA%A7%E7%94%9F%E5%86%B2%E7%AA%81%EF%BC%8Cmysql%E4%B9%9F%E6%98%AF%E7%B1%BB%E4%BC%BC%E5%8D%B8%E8%BD%BD%E6%96%B9%E5%BC%8F%EF%BC%89"><span class="nav-text">2.2 卸载mysql （下面是卸载mysql的库，防止产生冲突，mysql也是类似卸载方式）**</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-3-%E5%AE%89%E8%A3%85mysql"><span class="nav-text">2.3 安装mysql</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-4-%E5%90%AF%E5%8A%A8mysql"><span class="nav-text">2.4 启动mysql</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-5-root%E8%B4%A6%E6%88%B7%E9%BB%98%E8%AE%A4%E6%98%AF%E6%B2%A1%E6%9C%89%E5%AF%86%E7%A0%81%E7%9A%84%EF%BC%8C%E4%BF%AE%E6%94%B9root%E5%AF%86%E7%A0%81%EF%BC%9A"><span class="nav-text">2.5 root账户默认是没有密码的，修改root密码：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-6-%E9%87%8D%E7%BD%AEroot%E5%AF%86%E7%A0%81%EF%BC%88%E5%BF%98%E8%AE%B0root%E5%AF%86%E7%A0%81%E6%89%BE%E5%9B%9E%EF%BC%89"><span class="nav-text">2.6 重置root密码（忘记root密码找回）</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-6-1-%E5%81%9C%E6%AD%A2MySQL%E6%9C%8D%E5%8A%A1%E5%91%BD%E4%BB%A4"><span class="nav-text">2.6.1 停止MySQL服务命令:</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-6-2-%E8%BE%93%E5%85%A5%E7%BB%95%E8%BF%87%E5%AF%86%E7%A0%81%E8%AE%A4%E8%AF%81%E5%91%BD%E4%BB%A4%EF%BC%9A"><span class="nav-text">2.6.2 输入绕过密码认证命令：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-6-3-%E8%BE%93%E5%85%A5%E7%99%BB%E5%BD%95%E7%94%A8%E6%88%B7%E5%91%BD%E4%BB%A4%EF%BC%9A"><span class="nav-text">1.6.3 输入登录用户命令：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-6-4-%E8%BE%93%E5%85%A5%E4%BF%AE%E6%94%B9root%E5%AF%86%E7%A0%81SQL%E8%AF%AD%E5%8F%A5%EF%BC%9A"><span class="nav-text">2.6.4 输入修改root密码SQL语句：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-6-5-%E8%BE%93%E5%85%A5%E6%95%B0%E6%8D%AE%E5%88%B7%E6%96%B0%E5%91%BD%E4%BB%A4%EF%BC%9A"><span class="nav-text">2.6.5 输入数据刷新命令：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-6-6-%E9%80%80%E5%87%BAMySQL%E5%91%BD%E4%BB%A4%EF%BC%9A"><span class="nav-text">2.6.6 退出MySQL命令：</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-7-%E8%AE%BE%E7%BD%AE%E5%85%81%E8%AE%B8%E8%BF%9C%E7%A8%8B%E8%BF%9E%E6%8E%A5"><span class="nav-text">2.7 设置允许远程连接</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-8-%E5%BC%80%E6%94%BE%E7%AB%AF%E5%8F%A33306%EF%BC%8C%E5%90%A6%E5%88%99%E4%BE%9D%E7%84%B6%E6%97%A0%E6%B3%95%E8%BF%87%E8%BF%9C%E7%A8%8B"><span class="nav-text">2.8 开放端口3306，否则依然无法过远程</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-8-1-%E6%89%93%E5%BC%80%E9%98%B2%E7%81%AB%E5%A2%99%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%EF%BC%9A"><span class="nav-text">2.8.1 打开防火墙配置文件：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-8-2-%E6%B7%BB%E5%8A%A0%E4%B8%8B%E9%9D%A2%E4%B8%80%E8%A1%8C%EF%BC%9A"><span class="nav-text">2.8.2 添加下面一行：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-8-3-%E9%87%8D%E5%90%AF%E9%98%B2%E7%81%AB%E5%A2%99%EF%BC%8C%E4%BD%BF%E9%85%8D%E7%BD%AE%E7%94%9F%E6%95%88%EF%BC%9A"><span class="nav-text">2.8.3 重启防火墙，使配置生效：</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-9-%E8%AE%BE%E7%BD%AE%E5%BC%80%E6%9C%BA%E5%90%AF%E5%8A%A8mysql%EF%BC%9A"><span class="nav-text">2.9 设置开机启动mysql：</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-9-1-%E6%9F%A5%E7%9C%8BMySQL%E6%9C%8D%E5%8A%A1%E6%98%AF%E5%90%A6%E8%87%AA%E5%8A%A8%E5%BC%80%E5%90%AF%E5%91%BD%E4%BB%A4"><span class="nav-text">2.9.1 查看MySQL服务是否自动开启命令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-9-2-%E5%BC%80%E5%90%AFMySQL%E6%9C%8D%E5%8A%A1%E8%87%AA%E5%8A%A8%E5%BC%80%E5%90%AF%E5%91%BD%E4%BB%A4"><span class="nav-text">2.9.2 开启MySQL服务自动开启命令</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-10-%E5%B0%86mysql%E9%BB%98%E8%AE%A4%E5%BC%95%E6%93%8E%E8%AE%BE%E7%BD%AE%E4%B8%BAInnoDB"><span class="nav-text">2.10 将mysql默认引擎设置为InnoDB</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-11-%E5%BC%80%E5%90%AFmysql%E7%9A%84%E6%97%A5%E5%BF%97-%E7%9B%91%E6%8E%A7%E6%89%A7%E8%A1%8C%E7%9A%84sql%E8%AF%AD%E5%8F%A5"><span class="nav-text">2.11 开启mysql的日志(监控执行的sql语句)</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-12-centos7%E5%AE%89%E8%A3%85mysql"><span class="nav-text">2.12 centos7安装mysql</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#3-Mycat-%E5%AE%89%E8%A3%85%E6%95%99%E7%A8%8B"><span class="nav-text">3 Mycat 安装教程</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#3-1-%E5%AE%89%E8%A3%85-Mycat"><span class="nav-text">3.1 安装 Mycat</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-2-%E5%90%AF%E5%8A%A8%E5%92%8C%E9%AA%8C%E8%AF%81"><span class="nav-text">3.2 启动和验证</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-3-%E9%85%8D%E7%BD%AE"><span class="nav-text">3.3 配置</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-3-1-schema-xml"><span class="nav-text">3.3.1 schema.xml</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-3-2-server-xml"><span class="nav-text">3.3.2 server.xml</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-3-3-rule-xml"><span class="nav-text">3.3.3 rule.xml</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-4-Mycat-FAQ"><span class="nav-text">3.4 Mycat FAQ</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-4-1-ERROR-1184-HY000-Invalid-DataSource-1"><span class="nav-text">3.4.1 ERROR 1184 (HY000): Invalid DataSource:1</span></a></li></ol></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">一年春又来</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/blog/archives/">
        
          <span class="site-state-item-count">184</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/blog/categories/">
          
        <span class="site-state-item-count">35</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/blog/tags/">
          
        <span class="site-state-item-count">115</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/06/09/%E6%90%AD%E5%BB%BAMycat/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          搭建Mycat
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-06-09 20:51:05" itemprop="dateCreated datePublished" datetime="2021-06-09T20:51:05+08:00">2021-06-09</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-07-08 15:54:25" itemprop="dateModified" datetime="2021-07-08T15:54:25+08:00">2021-07-08</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/blog/categories/mycat/" itemprop="url" rel="index"><span itemprop="name">mycat</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="1-环境搭建"><a href="#1-环境搭建" class="headerlink" title="1 环境搭建"></a>1 环境搭建</h1><ol>
<li><p>3台服务器</p>
</li>
<li><p>centos 7</p>
</li>
<li><p>采用 yum 方式，在其中两台安装 mysql</p>
</li>
<li><p>检查mysql 安装是否正确</p>
</li>
<li><p>下载 Mycat 软件包</p>
</li>
<li><p>在第3台机器上安装mycat，并修改配置文件</p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/image-20210609210635587.png" alt="image-20210609210635587"></p>
</li>
<li><p>连接mycat，体验数据的增删改查</p>
</li>
</ol>
<h1 id="2-mysql-安装教程"><a href="#2-mysql-安装教程" class="headerlink" title="2 mysql 安装教程"></a>2 mysql 安装教程</h1><h2 id="2-1-查询是否安装了mysql"><a href="#2-1-查询是否安装了mysql" class="headerlink" title="2.1 查询是否安装了mysql**"></a>2.1 查询是否安装了mysql**</h2>  <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">rpm -qa|grep mysql                                                      </span><br></pre></td></tr></table></figure>

<h2 id="2-2-卸载mysql-（下面是卸载mysql的库，防止产生冲突，mysql也是类似卸载方式）"><a href="#2-2-卸载mysql-（下面是卸载mysql的库，防止产生冲突，mysql也是类似卸载方式）" class="headerlink" title="2.2 卸载mysql （下面是卸载mysql的库，防止产生冲突，mysql也是类似卸载方式）**"></a>2.2 卸载mysql （下面是卸载mysql的库，防止产生冲突，mysql也是类似卸载方式）**</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">rpm -e --nodeps mysql-libs-5.1.*</span><br><span class="line">卸载之后，记得：</span><br><span class="line">find &#x2F; -name mysql</span><br><span class="line">删除查询出来的所有东西</span><br></pre></td></tr></table></figure>

<h2 id="2-3-安装mysql"><a href="#2-3-安装mysql" class="headerlink" title="2.3 安装mysql"></a>2.3 安装mysql</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum install mysql-server                                                    </span><br></pre></td></tr></table></figure>

<p>注意: centos 7这样安装不行, 详见文档底部</p>
<h2 id="2-4-启动mysql"><a href="#2-4-启动mysql" class="headerlink" title="2.4 启动mysql"></a>2.4 启动mysql</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">启动方式1：service mysql start</span><br><span class="line">启动方式2：&#x2F;etc&#x2F;init.d&#x2F;mysql start</span><br><span class="line">启动方式3：service mysqld start</span><br><span class="line">启动方式4：&#x2F;etc&#x2F;init.d&#x2F;mysqld start                                               </span><br></pre></td></tr></table></figure>

<h2 id="2-5-root账户默认是没有密码的，修改root密码："><a href="#2-5-root账户默认是没有密码的，修改root密码：" class="headerlink" title="2.5 root账户默认是没有密码的，修改root密码："></a>2.5 root账户默认是没有密码的，修改root密码：</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;usr&#x2F;bin&#x2F;mysqladmin -u root password 密码 </span><br><span class="line">例如：</span><br><span class="line">&#x2F;usr&#x2F;bin&#x2F;mysqladmin -u root password pwd    这样就将root密码设置成pwd了       </span><br></pre></td></tr></table></figure>

<h2 id="2-6-重置root密码（忘记root密码找回）"><a href="#2-6-重置root密码（忘记root密码找回）" class="headerlink" title="2.6 重置root密码（忘记root密码找回）"></a>2.6 重置root密码（忘记root密码找回）</h2><h3 id="2-6-1-停止MySQL服务命令"><a href="#2-6-1-停止MySQL服务命令" class="headerlink" title="2.6.1 停止MySQL服务命令:"></a>2.6.1 停止MySQL服务命令:</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;etc&#x2F;init.d&#x2F;mysqld stop </span><br><span class="line">&#x2F;etc&#x2F;init.d&#x2F;mysql stop                                                        </span><br></pre></td></tr></table></figure>

<h3 id="2-6-2-输入绕过密码认证命令："><a href="#2-6-2-输入绕过密码认证命令：" class="headerlink" title="2.6.2 输入绕过密码认证命令："></a>2.6.2 输入绕过密码认证命令：</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mysqld_safe --user&#x3D;mysql --skip-grant-tables --skip-networking &amp;</span><br></pre></td></tr></table></figure>

<h3 id="1-6-3-输入登录用户命令："><a href="#1-6-3-输入登录用户命令：" class="headerlink" title="1.6.3 输入登录用户命令："></a>1.6.3 输入登录用户命令：</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mysql -u root mysql                                                        </span><br></pre></td></tr></table></figure>

<h3 id="2-6-4-输入修改root密码SQL语句："><a href="#2-6-4-输入修改root密码SQL语句：" class="headerlink" title="2.6.4 输入修改root密码SQL语句："></a>2.6.4 输入修改root密码SQL语句：</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">update user set Password&#x3D;password (&#39;123456&#39;) where user&#x3D;&#39;root&#39;;          </span><br></pre></td></tr></table></figure>

<h3 id="2-6-5-输入数据刷新命令："><a href="#2-6-5-输入数据刷新命令：" class="headerlink" title="2.6.5 输入数据刷新命令："></a>2.6.5 输入数据刷新命令：</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">FLUSH PRIVILEGES;                                                   </span><br></pre></td></tr></table></figure>

<h3 id="2-6-6-退出MySQL命令："><a href="#2-6-6-退出MySQL命令：" class="headerlink" title="2.6.6 退出MySQL命令："></a>2.6.6 退出MySQL命令：</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">quit;                                                              </span><br></pre></td></tr></table></figure>

<h2 id="2-7-设置允许远程连接"><a href="#2-7-设置允许远程连接" class="headerlink" title="2.7 设置允许远程连接"></a>2.7 设置允许远程连接</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">grant all privileges on *.* to root@&#39;%&#39; identified by &#39;123456789&#39; with grant option;  </span><br></pre></td></tr></table></figure>

<h2 id="2-8-开放端口3306，否则依然无法过远程"><a href="#2-8-开放端口3306，否则依然无法过远程" class="headerlink" title="2.8 开放端口3306，否则依然无法过远程"></a>2.8 开放端口3306，否则依然无法过远程</h2><h3 id="2-8-1-打开防火墙配置文件："><a href="#2-8-1-打开防火墙配置文件：" class="headerlink" title="2.8.1 打开防火墙配置文件："></a>2.8.1 打开防火墙配置文件：</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vi &#x2F;etc&#x2F;sysconfig&#x2F;iptables                                                   </span><br></pre></td></tr></table></figure>

<h3 id="2-8-2-添加下面一行："><a href="#2-8-2-添加下面一行：" class="headerlink" title="2.8.2 添加下面一行："></a>2.8.2 添加下面一行：</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT</span><br></pre></td></tr></table></figure>

<p>注意：开通3306 端口的行必须在icmp-host-prohibited前，否则无效：以下为配置结果图：</p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/5e81a3ba087c42ce05540189.jpg" alt="图片描述"></p>
<h3 id="2-8-3-重启防火墙，使配置生效："><a href="#2-8-3-重启防火墙，使配置生效：" class="headerlink" title="2.8.3 重启防火墙，使配置生效："></a>2.8.3 重启防火墙，使配置生效：</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;etc&#x2F;init.d&#x2F;iptables restart                                                </span><br></pre></td></tr></table></figure>

<h2 id="2-9-设置开机启动mysql："><a href="#2-9-设置开机启动mysql：" class="headerlink" title="2.9 设置开机启动mysql："></a>2.9 设置开机启动mysql：</h2><h3 id="2-9-1-查看MySQL服务是否自动开启命令"><a href="#2-9-1-查看MySQL服务是否自动开启命令" class="headerlink" title="2.9.1 查看MySQL服务是否自动开启命令"></a>2.9.1 查看MySQL服务是否自动开启命令</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">chkconfig --list | grep mysqld</span><br><span class="line">chkconfig --list | grep mysql                                            </span><br></pre></td></tr></table></figure>

<h3 id="2-9-2-开启MySQL服务自动开启命令"><a href="#2-9-2-开启MySQL服务自动开启命令" class="headerlink" title="2.9.2 开启MySQL服务自动开启命令"></a>2.9.2 开启MySQL服务自动开启命令</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">chkconfig mysqld on</span><br><span class="line">chkconfig mysql on                                              </span><br></pre></td></tr></table></figure>

<h2 id="2-10-将mysql默认引擎设置为InnoDB"><a href="#2-10-将mysql默认引擎设置为InnoDB" class="headerlink" title="2.10 将mysql默认引擎设置为InnoDB"></a>2.10 将mysql默认引擎设置为InnoDB</h2><p>修改MySQL配置文件my.cnf</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">cd &#x2F;etc</span><br><span class="line">vi my.cnf                                                         </span><br></pre></td></tr></table></figure>

<p>在[mysqld]一段加入</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">default-storage-engine&#x3D;InnoDB                                            </span><br></pre></td></tr></table></figure>

<p>删除ib_logfile0、ib_logfile1两个文件</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">cd &#x2F;var&#x2F;lib&#x2F;mysql</span><br><span class="line">rm -rf ib_logfile*                                                   </span><br></pre></td></tr></table></figure>

<p>重启mysql</p>
<h2 id="2-11-开启mysql的日志-监控执行的sql语句"><a href="#2-11-开启mysql的日志-监控执行的sql语句" class="headerlink" title="2.11 开启mysql的日志(监控执行的sql语句)"></a>2.11 开启mysql的日志(监控执行的sql语句)</h2><p>命令: <code>show global variables like ‘%general%’;</code> 该语句可以查看是否开启, 以及生成的位置</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">set global general_log &#x3D; on; &#x2F;&#x2F; 打开  </span><br><span class="line">set global general_log &#x3D; off; &#x2F;&#x2F; 关闭      </span><br></pre></td></tr></table></figure>

<p>参考文档:</p>
<p><a target="_blank" rel="noopener" href="http://blog.csdn.net/fdipzone/article/details/16995303">http://blog.csdn.net/fdipzone/article/details/16995303</a></p>
<h2 id="2-12-centos7安装mysql"><a href="#2-12-centos7安装mysql" class="headerlink" title="2.12 centos7安装mysql"></a>2.12 centos7安装mysql</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">wget http:&#x2F;&#x2F;dev.mysql.com&#x2F;get&#x2F;mysql-community-release-el7-5.noarch.rpm</span><br><span class="line">#rpm -ivh mysql-community-release-el7-5.noarch.rpm</span><br><span class="line">#yum install mysql-community-server</span><br><span class="line">成功安装之后重启mysql服务</span><br><span class="line">#service mysqld restart</span><br><span class="line">初次安装mysql是root账户是没有密码的</span><br><span class="line">设置密码的方法</span><br><span class="line">#mysql -uroot</span><br><span class="line">mysql&gt; set password for ‘root’@‘localhost’ &#x3D; password(&#39;mypasswd&#39;);</span><br><span class="line">mysql&gt; exit</span><br></pre></td></tr></table></figure>

<h1 id="3-Mycat-安装教程"><a href="#3-Mycat-安装教程" class="headerlink" title="3 Mycat 安装教程"></a>3 Mycat 安装教程</h1><p><a target="_blank" rel="noopener" href="http://www.mycat.org.cn/">mycat官网</a></p>
<p><a target="_blank" rel="noopener" href="https://www.yuque.com/ccazhw/tuacvk">mycat1权威指南</a></p>
<p><a target="_blank" rel="noopener" href="https://www.yuque.com/ccazhw/ml3nkf">mycat2权威指南</a></p>
<h2 id="3-1-安装-Mycat"><a href="#3-1-安装-Mycat" class="headerlink" title="3.1 安装 Mycat"></a>3.1 安装 Mycat</h2><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">tar -zxvf Mycat-server-1.6.7.1-release-20200209222254-mac.tar</span><br></pre></td></tr></table></figure>

<h2 id="3-2-启动和验证"><a href="#3-2-启动和验证" class="headerlink" title="3.2 启动和验证"></a>3.2 启动和验证</h2><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">bin/mycat start</span><br></pre></td></tr></table></figure>

<p>启动后，验证一些基本操作，如下图所示：</p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/image-20210610162658349.png" alt="image-20210610162658349"></p>
<p>可以看到，我们成功连上了 mycat 服务器，MyCat 服务器默认定义了一个名为 TESTDB 的逻辑数据库，并且也在该逻辑数据库中定义了一些逻辑表。</p>
<p>但当我们尝试做一些 select 操作的时候，控制台会提示报错，这是因为 MyCat 配置错误导致的。</p>
<p>所以我们需要进行配置。</p>
<h2 id="3-3-配置"><a href="#3-3-配置" class="headerlink" title="3.3 配置"></a>3.3 配置</h2><p><img src="https://gitee.com/littlefxc/oss/raw/master/images/image-20210610163531656.png" alt="image-20210610163531656"></p>
<p>其中 ,</p>
<ul>
<li>bin 目录是 MyCat 的启动目录，</li>
<li>conf 目录是 MyCat 的配置文件目录，</li>
<li>lib 目录是 MyCat 自身的 Jar 包以及所依赖 Jar 包的目录，</li>
<li>logs 目录是日志目录。</li>
</ul>
<p>在 conf 目录下有 3 个重要的配置文件：</p>
<ul>
<li>schema.xml</li>
<li>Server.xml</li>
<li>Rule.xml</li>
</ul>
<p>下面就来简单说明这 3 个配置文件的关键配置</p>
<h3 id="3-3-1-schema-xml"><a href="#3-3-1-schema-xml" class="headerlink" title="3.3.1 schema.xml"></a>3.3.1 schema.xml</h3><p>schema.xml 文件定义了 MyCat 到底连接那个数据库实例，连接这个数据库实例的哪个数据库。MyCat 一共有几个逻辑数据库，MyCat 一共有几个逻辑表。</p>
<p>schema.xml 文件一共有四个配置节点：<code>DataHost</code>、<code>DataNode</code>、<code>Schema</code>、<code>Table</code>。</p>
<ul>
<li><p>DataHost：定义数据库实例</p>
<ul>
<li><p>balance：负载均衡类型</p>
<ul>
<li>balance=”0”, 不开启读写分离机制，所有读操作都发送到当前可用的writeHost上。</li>
<li>balance=”1”，全部的readHost与stand by writeHost参与select语句的负载均衡，简单的说，当双主双从模式(M1-&gt;S1，M2-&gt;S2，并且M1与 M2互为主备)，正常情况下，M2,S1,S2都参与select语句的负载均衡。</li>
<li>balance=”2”，所有读操作都随机的在writeHost、readhost上分发。</li>
<li>balance=”3”，所有读请求随机的分发到wiriterHost对应的readhost执行，writerHost不负担读压力，注意balance=3只在1.4及其以后版本有，1.3没有。</li>
</ul>
</li>
<li><p>writeType：写请求类型，0落在第一个writeHost上；1随机；</p>
</li>
</ul>
</li>
<li><p>DataNode：定义数据库名称</p>
</li>
<li><p>Schema：定义逻辑库</p>
<ul>
<li><p>checkSQLschema：是否去掉SQL中的schema</p>
</li>
<li><p>sqlMaxLimit：select 默认的<code>limit</code>值，仅对分片表有效</p>
</li>
<li><p>rule：定义分片表的分片规则，必须与<code>rule.xml</code>中的<code>tableRule</code>对应</p>
</li>
<li><p>ruleRequired：是否绑定分片规则，如果为true，没有绑定分片规则，程序报错</p>
</li>
</ul>
</li>
<li><p>Table：定义逻辑表</p>
</li>
</ul>
<p>DataHost 节点定义了 MyCat 要连接哪个 MySQL 实例，连接的账号密码是多少。默认的 MyCat 为我们定义了一个名为 localhost1 的数据服务器（DataHost），它指向了本地（localhost）3306 端口的 MySQL 服务器，对应 MySQL 服务器的账号是 root，密码是 123456。</p>
  <figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dataHost</span> <span class="attr">name</span>=<span class="string">&quot;localhost1&quot;</span> <span class="attr">maxCon</span>=<span class="string">&quot;1000&quot;</span> <span class="attr">minCon</span>=<span class="string">&quot;10&quot;</span> <span class="attr">balance</span>=<span class="string">&quot;0&quot;</span> <span class="attr">writeType</span>=<span class="string">&quot;0&quot;</span> <span class="attr">dbType</span>=<span class="string">&quot;mysql&quot;</span> <span class="attr">dbDriver</span>=<span class="string">&quot;native&quot;</span> <span class="attr">switchType</span>=<span class="string">&quot;1&quot;</span>  <span class="attr">slaveThreshold</span>=<span class="string">&quot;100&quot;</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">heartbeat</span>&gt;</span>select user()<span class="tag">&lt;/<span class="name">heartbeat</span>&gt;</span> </span><br><span class="line">	<span class="tag">&lt;<span class="name">writeHost</span> <span class="attr">host</span>=<span class="string">&quot;hostM1&quot;</span> <span class="attr">url</span>=<span class="string">&quot;localhost:3306&quot;</span> <span class="attr">user</span>=<span class="string">&quot;root&quot;</span> <span class="attr">password</span>=<span class="string">&quot;123456&quot;</span>&gt;</span> </span><br><span class="line">		<span class="tag">&lt;<span class="name">readHost</span> <span class="attr">host</span>=<span class="string">&quot;hostS2&quot;</span> <span class="attr">url</span>=<span class="string">&quot;192.168.1.200:3306&quot;</span> <span class="attr">user</span>=<span class="string">&quot;root&quot;</span> <span class="attr">password</span>=<span class="string">&quot;xxx&quot;</span> /&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">writeHost</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">writeHost</span> <span class="attr">host</span>=<span class="string">&quot;hostS1&quot;</span> <span class="attr">url</span>=<span class="string">&quot;localhost:3316&quot;</span> <span class="attr">user</span>=<span class="string">&quot;root&quot;</span> <span class="attr">password</span>=<span class="string">&quot;123456&quot;</span> /&gt;</span> </span><br><span class="line"><span class="tag">&lt;/<span class="name">dataHost</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>DataNode 节点指定了需要连接的具体数据库名称，其使用一个 dataHost 属性指定该数据库位于哪个数据库实例上。默认的 MyCat 为我们创建了三个数据节点（DataNode），dn1 数据节点对应 localhost1 数据服务器上的 db1 数据库，dn2 数据节点对应 localhost1 数据服务器上的 db2 数据库，dn1 数据节点对应 localhost1 数据服务器上的 db3 数据库。</p>
  <figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dataNode</span> <span class="attr">name</span>=<span class="string">&quot;dn1&quot;</span> <span class="attr">dataHost</span>=<span class="string">&quot;localhost1&quot;</span> <span class="attr">database</span>=<span class="string">&quot;db1&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dataNode</span> <span class="attr">name</span>=<span class="string">&quot;dn2&quot;</span> <span class="attr">dataHost</span>=<span class="string">&quot;localhost1&quot;</span> <span class="attr">database</span>=<span class="string">&quot;db2&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dataNode</span> <span class="attr">name</span>=<span class="string">&quot;dn3&quot;</span> <span class="attr">dataHost</span>=<span class="string">&quot;localhost1&quot;</span> <span class="attr">database</span>=<span class="string">&quot;db3&quot;</span> /&gt;</span></span><br></pre></td></tr></table></figure>

<p>Schema 节点定义了 MyCat 的所有逻辑数据库，Table 节点定义了 MyCat 的所有逻辑表。默认的 MyCat 为我们定义了一个名为 TESTDB 的逻辑数据库，在这个逻辑数据库下又定义了名为 travaelrecord、company 等 6 个逻辑表。</p>
  <figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">schema</span> <span class="attr">name</span>=<span class="string">&quot;TESTDB&quot;</span> <span class="attr">checkSQLschema</span>=<span class="string">&quot;false&quot;</span> <span class="attr">sqlMaxLimit</span>=<span class="string">&quot;100&quot;</span>&gt;</span></span><br><span class="line">		<span class="comment">&lt;!-- auto sharding by id (long) --&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">table</span> <span class="attr">name</span>=<span class="string">&quot;travelrecord&quot;</span> <span class="attr">dataNode</span>=<span class="string">&quot;dn1,dn2,dn3&quot;</span> <span class="attr">rule</span>=<span class="string">&quot;auto-sharding-long&quot;</span> /&gt;</span></span><br><span class="line"></span><br><span class="line">		<span class="comment">&lt;!-- global table is auto cloned to all defined data nodes ,so can join</span></span><br><span class="line"><span class="comment">			with any table whose sharding node is in the same data node --&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">table</span> <span class="attr">name</span>=<span class="string">&quot;company&quot;</span> <span class="attr">primaryKey</span>=<span class="string">&quot;ID&quot;</span> <span class="attr">type</span>=<span class="string">&quot;global&quot;</span> <span class="attr">dataNode</span>=<span class="string">&quot;dn1,dn2,dn3&quot;</span> /&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">table</span> <span class="attr">name</span>=<span class="string">&quot;goods&quot;</span> <span class="attr">primaryKey</span>=<span class="string">&quot;ID&quot;</span> <span class="attr">type</span>=<span class="string">&quot;global&quot;</span> <span class="attr">dataNode</span>=<span class="string">&quot;dn1,dn2&quot;</span> /&gt;</span></span><br><span class="line">		<span class="comment">&lt;!-- random sharding using mod sharind rule --&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">table</span> <span class="attr">name</span>=<span class="string">&quot;hotnews&quot;</span> <span class="attr">primaryKey</span>=<span class="string">&quot;ID&quot;</span> <span class="attr">autoIncrement</span>=<span class="string">&quot;true&quot;</span> <span class="attr">dataNode</span>=<span class="string">&quot;dn1,dn2,dn3&quot;</span></span></span><br><span class="line"><span class="tag">			   <span class="attr">rule</span>=<span class="string">&quot;mod-long&quot;</span> /&gt;</span></span><br><span class="line">		<span class="comment">&lt;!-- &lt;table name=&quot;dual&quot; primaryKey=&quot;ID&quot; dataNode=&quot;dnx,dnoracle2&quot; type=&quot;global&quot;</span></span><br><span class="line"><span class="comment">			needAddLimit=&quot;false&quot;/&gt; &lt;table name=&quot;worker&quot; primaryKey=&quot;ID&quot; dataNode=&quot;jdbc_dn1,jdbc_dn2,jdbc_dn3&quot;</span></span><br><span class="line"><span class="comment">			rule=&quot;mod-long&quot; /&gt; --&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">table</span> <span class="attr">name</span>=<span class="string">&quot;employee&quot;</span> <span class="attr">primaryKey</span>=<span class="string">&quot;ID&quot;</span> <span class="attr">dataNode</span>=<span class="string">&quot;dn1,dn2&quot;</span></span></span><br><span class="line"><span class="tag">			   <span class="attr">rule</span>=<span class="string">&quot;sharding-by-intfile&quot;</span> /&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">table</span> <span class="attr">name</span>=<span class="string">&quot;customer&quot;</span> <span class="attr">primaryKey</span>=<span class="string">&quot;ID&quot;</span> <span class="attr">dataNode</span>=<span class="string">&quot;dn1,dn2&quot;</span></span></span><br><span class="line"><span class="tag">			   <span class="attr">rule</span>=<span class="string">&quot;sharding-by-intfile&quot;</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">childTable</span> <span class="attr">name</span>=<span class="string">&quot;orders&quot;</span> <span class="attr">primaryKey</span>=<span class="string">&quot;ID&quot;</span> <span class="attr">joinKey</span>=<span class="string">&quot;customer_id&quot;</span></span></span><br><span class="line"><span class="tag">						<span class="attr">parentKey</span>=<span class="string">&quot;id&quot;</span>&gt;</span></span><br><span class="line">				<span class="tag">&lt;<span class="name">childTable</span> <span class="attr">name</span>=<span class="string">&quot;order_items&quot;</span> <span class="attr">joinKey</span>=<span class="string">&quot;order_id&quot;</span></span></span><br><span class="line"><span class="tag">							<span class="attr">parentKey</span>=<span class="string">&quot;id&quot;</span> /&gt;</span></span><br><span class="line">			<span class="tag">&lt;/<span class="name">childTable</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">childTable</span> <span class="attr">name</span>=<span class="string">&quot;customer_addr&quot;</span> <span class="attr">primaryKey</span>=<span class="string">&quot;ID&quot;</span> <span class="attr">joinKey</span>=<span class="string">&quot;customer_id&quot;</span></span></span><br><span class="line"><span class="tag">						<span class="attr">parentKey</span>=<span class="string">&quot;id&quot;</span> /&gt;</span></span><br><span class="line">		<span class="tag">&lt;/<span class="name">table</span>&gt;</span></span><br><span class="line">		<span class="comment">&lt;!-- &lt;table name=&quot;oc_call&quot; primaryKey=&quot;ID&quot; dataNode=&quot;dn1$0-743&quot; rule=&quot;latest-month-calldate&quot;</span></span><br><span class="line"><span class="comment">			/&gt; --&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">schema</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>所以上面当我们登陆 MyCat 输入<code>show databases</code>会看到只有一个名为 TESTDB 的数据库，这个就是 MyCat 的逻辑数据库。</p>
<h3 id="3-3-2-server-xml"><a href="#3-3-2-server-xml" class="headerlink" title="3.3.2 server.xml"></a>3.3.2 server.xml</h3><p>server.xml 定义了项目中连接 MyCat 服务器所需要的账号密码，以及该账号能访问那些逻辑数据库。 server.xml 配置文件中有 <code>System</code> 和 <code>User</code> 两个配置节点。</p>
<p>System 节点定义了连接 MyCat 服务器的系统配置信息。例如是否开启实时统计功能，是否开启全加班一致性检测等。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">system</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;useSqlStat&quot;</span>&gt;</span>0<span class="tag">&lt;/<span class="name">property</span>&gt;</span>  <span class="comment">&lt;!-- 1为开启实时统计、0为关闭 --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;useGlobleTableCheck&quot;</span>&gt;</span>0<span class="tag">&lt;/<span class="name">property</span>&gt;</span>  <span class="comment">&lt;!-- 1为开启全加班一致性检测、0为关闭 --&gt;</span> </span><br><span class="line">	<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;sequnceHandlerType&quot;</span>&gt;</span>2<span class="tag">&lt;/<span class="name">property</span>&gt;</span> </span><br><span class="line">	<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;processorBufferPoolType&quot;</span>&gt;</span>0<span class="tag">&lt;/<span class="name">property</span>&gt;</span> </span><br><span class="line">	……</span><br><span class="line"><span class="tag">&lt;/<span class="name">system</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>User 配置节点定义了连接 MyCat 服务器的账号密码，以及该账号密码所能进行的数据库操作。默认的 MyCat 为我们创建了一个账户名为 root，密码为 123456 的账号，只能访问 TESTDB 逻辑数据库，并且定义了对相关表的操作权限。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">user</span> <span class="attr">name</span>=<span class="string">&quot;root&quot;</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;password&quot;</span>&gt;</span>123456<span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;schemas&quot;</span>&gt;</span>TESTDB<span class="tag">&lt;/<span class="name">property</span>&gt;</span> </span><br><span class="line">	<span class="tag">&lt;<span class="name">privileges</span> <span class="attr">check</span>=<span class="string">&quot;false&quot;</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">schema</span> <span class="attr">name</span>=<span class="string">&quot;TESTDB&quot;</span> <span class="attr">dml</span>=<span class="string">&quot;0110&quot;</span> &gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">table</span> <span class="attr">name</span>=<span class="string">&quot;tb01&quot;</span> <span class="attr">dml</span>=<span class="string">&quot;0000&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">table</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">table</span> <span class="attr">name</span>=<span class="string">&quot;tb02&quot;</span> <span class="attr">dml</span>=<span class="string">&quot;1111&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">table</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;/<span class="name">schema</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">privileges</span>&gt;</span>	 </span><br><span class="line"><span class="tag">&lt;/<span class="name">user</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="3-3-3-rule-xml"><a href="#3-3-3-rule-xml" class="headerlink" title="3.3.3 rule.xml"></a>3.3.3 rule.xml</h3><p>rule.xml 定义了逻辑表使用哪个字段进行拆分，使用什么拆分算法进行拆分。rule.xml 中有两个配置节点，分别是：<code>TableRule</code> 和 <code>Function</code> 配置节点。</p>
<p>TableRule 配置节点定义了逻辑表的拆分信息，例如使用哪个字段进行拆分，使用什么拆分算法。默认的 MyCat 为我们配置了一个名为 rule2 的表拆分规则，表示根据 user_id 字段进行拆分，拆分算法是 func1。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">tableRule</span> <span class="attr">name</span>=<span class="string">&quot;rule2&quot;</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">rule</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">columns</span>&gt;</span>user_id<span class="tag">&lt;/<span class="name">columns</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">algorithm</span>&gt;</span>func1<span class="tag">&lt;/<span class="name">algorithm</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">rule</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">tableRule</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>Function 配置节点则定义了具体的拆分算法。例如使用对 1000 取余的拆分算法，对 100 取余的拆分算分等等。默认的 MyCat 为我们定义了一个名为<code> func1</code> 的<strong>拆分算法</strong>，这个拆分算法定义在 <code>io.mycat.route.function.PartitionByLong</code> 类中，并且还传入了两个参数值。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">function</span> <span class="attr">name</span>=<span class="string">&quot;func1&quot;</span> <span class="attr">class</span>=<span class="string">&quot;io.mycat.route.function.PartitionByLong&quot;</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;partitionCount&quot;</span>&gt;</span>8<span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;partitionLength&quot;</span>&gt;</span>128<span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">function</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h2 id="3-4-Mycat-FAQ"><a href="#3-4-Mycat-FAQ" class="headerlink" title="3.4 Mycat FAQ"></a>3.4 Mycat FAQ</h2><h3 id="3-4-1-ERROR-1184-HY000-Invalid-DataSource-1"><a href="#3-4-1-ERROR-1184-HY000-Invalid-DataSource-1" class="headerlink" title="3.4.1 ERROR 1184 (HY000): Invalid DataSource:1"></a>3.4.1 ERROR 1184 (HY000): Invalid DataSource:1</h3><p>具体错误如下：</p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/image-20210611093234112.png" alt="image-20210611093234112"></p>
<p>错误原因有两种可能：</p>
<ol>
<li><p>没有为mysql用户配置远程访问的权限</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">writeHost</span> <span class="attr">host</span>=<span class="string">&quot;db1&quot;</span> <span class="attr">url</span>=<span class="string">&quot;192.168.0.3:3306&quot;</span> <span class="attr">user</span>=<span class="string">&quot;root&quot;</span> <span class="attr">password</span>=<span class="string">&quot;123456&quot;</span> /&gt;</span></span><br></pre></td></tr></table></figure>

<p>授予mysql用户远程访问的权限</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">   <span class="keyword">GRANT</span> <span class="keyword">ALL</span> PRIVILEGES <span class="keyword">ON</span> <span class="operator">*</span>.<span class="operator">*</span> <span class="keyword">TO</span> <span class="string">&#x27;root&#x27;</span>@<span class="string">&#x27;%&#x27;</span> IDENTIFIED <span class="keyword">BY</span> <span class="string">&#x27;123456&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="number">2.</span> 没有在mysql中创建数据库</span><br><span class="line"></span><br><span class="line">   ```<span class="keyword">sql</span></span><br><span class="line">   # 此操作在当前机的mysql上操作（不再mycat）</span><br><span class="line">   # mysql <span class="operator">-</span>uroot <span class="operator">-</span>p</span><br><span class="line">   <span class="keyword">CREATE</span> DATABASE IF <span class="keyword">NOT</span> <span class="keyword">EXISTS</span> mycatdb1 <span class="keyword">DEFAULT</span> CHARSET utf8mb4 <span class="keyword">COLLATE</span> utf8mb4_general_ci;</span><br><span class="line">   <span class="keyword">CREATE</span> DATABASE IF <span class="keyword">NOT</span> <span class="keyword">EXISTS</span> mycatdb2 <span class="keyword">DEFAULT</span> CHARSET utf8mb4 <span class="keyword">COLLATE</span> utf8mb4_general_ci;</span><br><span class="line">   <span class="keyword">CREATE</span> DATABASE IF <span class="keyword">NOT</span> <span class="keyword">EXISTS</span> mycatdb3 <span class="keyword">DEFAULT</span> CHARSET utf8mb4 <span class="keyword">COLLATE</span> utf8mb4_general_ci;</span><br><span class="line">   </span><br><span class="line">   # 三个分库各自创建表travelrecord</span><br><span class="line">   <span class="keyword">CREATE</span> <span class="keyword">TABLE</span> `travelrecord` (</span><br><span class="line">     `id` <span class="type">int</span>(<span class="number">10</span>) unsigned <span class="keyword">NOT</span> <span class="keyword">NULL</span> AUTO_INCREMENT,</span><br><span class="line">     `name` <span class="type">varchar</span>(<span class="number">22</span>) <span class="keyword">NOT</span> <span class="keyword">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">&#x27;&#x27;</span>,</span><br><span class="line">     `<span class="type">time</span>` <span class="type">int</span>(<span class="number">10</span>) unsigned <span class="keyword">NOT</span> <span class="keyword">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">&#x27;0&#x27;</span>,</span><br><span class="line">     <span class="keyword">PRIMARY</span> KEY (`id`)</span><br><span class="line">   ) ENGINE<span class="operator">=</span>InnoDB AUTO_INCREMENT<span class="operator">=</span><span class="number">1</span> <span class="keyword">DEFAULT</span> CHARSET<span class="operator">=</span>utf8mb4;</span><br><span class="line">   </span><br><span class="line">   # 模拟数据</span><br><span class="line">   <span class="keyword">INSERT</span> <span class="keyword">INTO</span> `mycat<span class="operator">-</span>db1`.`travelrecord` (`name`, `<span class="type">time</span>`) <span class="keyword">VALUES</span> (<span class="string">&#x27;qkl&#x27;</span>, <span class="string">&#x27;0&#x27;</span>);</span><br><span class="line">   <span class="keyword">INSERT</span> <span class="keyword">INTO</span> `mycat<span class="operator">-</span>db1`.`travelrecord` (`name`, `<span class="type">time</span>`) <span class="keyword">VALUES</span> (<span class="string">&#x27;andy&#x27;</span>, <span class="string">&#x27;0&#x27;</span>);</span><br><span class="line">   <span class="keyword">INSERT</span> <span class="keyword">INTO</span> `mycat<span class="operator">-</span>db2`.`travelrecord` (`name`, `<span class="type">time</span>`) <span class="keyword">VALUES</span> (<span class="string">&#x27;zgq&#x27;</span>, <span class="string">&#x27;0&#x27;</span>);</span><br><span class="line">   <span class="keyword">INSERT</span> <span class="keyword">INTO</span> `mycat<span class="operator">-</span>db3`.`travelrecord` (`name`, `<span class="type">time</span>`) <span class="keyword">VALUES</span> (<span class="string">&#x27;pcb&#x27;</span>, <span class="string">&#x27;0&#x27;</span>);</span><br></pre></td></tr></table></figure>

<p>在 mycat 的配置文件 schema.xml 中修改 <code>dataNode</code>节点：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dataNode</span> <span class="attr">name</span>=<span class="string">&quot;dn1&quot;</span> <span class="attr">dataHost</span>=<span class="string">&quot;localhost1&quot;</span> <span class="attr">database</span>=<span class="string">&quot;mycatdb1&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dataNode</span> <span class="attr">name</span>=<span class="string">&quot;dn2&quot;</span> <span class="attr">dataHost</span>=<span class="string">&quot;localhost1&quot;</span> <span class="attr">database</span>=<span class="string">&quot;mycatdb2&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dataNode</span> <span class="attr">name</span>=<span class="string">&quot;dn3&quot;</span> <span class="attr">dataHost</span>=<span class="string">&quot;localhost1&quot;</span> <span class="attr">database</span>=<span class="string">&quot;mycatdb3&quot;</span> /&gt;</span></span><br></pre></td></tr></table></figure></li>
</ol>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/blog/tags/%E8%AF%BB%E5%86%99%E5%88%86%E7%A6%BB/" rel="tag"># 读写分离</a>
              <a href="/blog/tags/%E5%88%86%E5%BA%93%E5%88%86%E8%A1%A8/" rel="tag"># 分库分表</a>
              <a href="/blog/tags/mycat/" rel="tag"># mycat</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/blog/2021/06/09/Netty%E7%9A%84%E6%A0%B8%E5%BF%83%E7%BB%84%E4%BB%B6/" rel="prev" title="Netty的核心组件">
                  <i class="fa fa-chevron-left"></i> Netty的核心组件
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/blog/2021/06/20/MySQL%E4%B8%BB%E4%BB%8E%E6%90%AD%E5%BB%BA/" rel="next" title="MySQL主从搭建">
                  MySQL主从搭建 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>







<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">一年春又来</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/mist/" class="theme-link" rel="noopener" target="_blank">NexT.Mist</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/blog/js/utils.js"></script><script src="/blog/js/motion.js"></script><script src="/blog/js/schemes/muse.js"></script><script src="/blog/js/next-boot.js"></script>

  
<script src="/blog/js/local-search.js"></script>






  





</body>
</html>
